package com.telecom.restaurant.controllor.user;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.UserThreadLocal;
import com.telecom.restaurant.dao.CustomerRestaurantChoiceDao;
import com.telecom.restaurant.dao.OrderDao;
import com.telecom.restaurant.dao.RestaurantDao;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.domin.vo.DailyMenuVO;
import com.telecom.restaurant.enums.OrderTypeEnum;
import com.telecom.restaurant.model.CustomerRestaurantChoicePO;
import com.telecom.restaurant.model.RestaurantPO;
import com.telecom.restaurant.model.param.OrderSearchParam;
import com.telecom.restaurant.service.CustomerService;
import com.telecom.restaurant.service.MenuService;
import com.telecom.restaurant.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by qiqi on 2019/12/15.
 */
@Controller
@RequestMapping("/user/restaurant")
public class UserRestaurantController {

    @Value("#{prop.STATISTIC_USER_ID}")
    private String STATISTIC_USER_ID;


    @Autowired
    private RestaurantDao restaurantDao;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private CustomerRestaurantChoiceDao customerRestaurantChoiceDao;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private OrderDao orderDao;


    @RequestMapping("/list")
    @ResponseBody
    public DataResult getRestaurantList() {
        List<RestaurantPO> poList = restaurantDao.getAll();
        pictureService.renderRestaurantList(poList);
        DataResult dataResult = DataResultConstant.buildSuccessResult(poList);
        return dataResult;
    }


    @RequestMapping("/choice")
    @ResponseBody
    public DataResult choseRestaurant(Long restaurantId) {

        Long userId = UserThreadLocal.userId.get();

        if (restaurantId == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        CustomerRestaurantChoicePO customerRestaurantChoicePO = new CustomerRestaurantChoicePO();
        customerRestaurantChoicePO.setRestaurantId(restaurantId);
        customerRestaurantChoicePO.setUserId(userId);

        customerRestaurantChoiceDao.insert(customerRestaurantChoicePO);

        DataResult dataResult = DataResultConstant.buildSuccessResult(null);
        return dataResult;
    }


    @RequestMapping("/dayMenu")
    @ResponseBody
    public DataResult getDayMenu(Integer dayNo) {
        if (dayNo == null || dayNo < 0 || dayNo > 4) {
            return DataResultConstant.WRONG_PARAM;
        }

        Long userId = UserThreadLocal.userId.get();

        Long restaurantId = customerService.getUserChooseRestaurant(userId);

        // 查询某天菜单
        DailyMenuVO dailyMenuVO = menuService.getOneDayMenu(restaurantId, dayNo);

        DataResult dataResult = DataResultConstant.buildSuccessResult(dailyMenuVO);
        return dataResult;
    }


    @RequestMapping("/dayOrderCount")
    @ResponseBody
    public DataResult dayOrderCount() {

        int count = 0;

        String userMobile = UserThreadLocal.mobile.get();

        // check user id
        if(this.STATISTIC_USER_ID == null){
            return DataResultConstant.NO_PERMISSION;
        }
        String[] userIds = this.STATISTIC_USER_ID.split(",");
        boolean hasRule = false;
        if(userIds.length > 0){
            for(String userIdSetting : userIds){
                if(userMobile.equals(userIdSetting)){
                    hasRule = true;
                    break;
                }
            }
        }
        if(!hasRule){
            return DataResultConstant.NO_PERMISSION;
        }

        Long restaurantId = 1L;

        OrderSearchParam param = new OrderSearchParam();
        param.setRestaurantId(restaurantId);
        param.setOrderType(OrderTypeEnum.BOOK.getCode());
        param.setOrderDate(new Date());
        count = orderDao.count(param);

        DataResult dataResult = DataResultConstant.buildSuccessResult(count);
        return dataResult;
    }


}
